#include <my_header.h>
#include "log.h"

// 初始化日志系统
int init_log_system() {
    openlog("CloudDisk", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
    syslog(LOG_NOTICE, "===== Cloud Disk Service Started =====");
    return 0;
}

// 记录客户端连接
int log_connection(struct sockaddr *addr) {
    struct sockaddr_in *client_addr = (struct sockaddr_in *)addr;
    char ip[INET_ADDRSTRLEN];
    char *ip_in = inet_ntoa(client_addr->sin_addr);
    strncpy(ip,ip_in,INET_ADDRSTRLEN);
    int port = ntohs(client_addr->sin_port);

    syslog(LOG_NOTICE, "CONNECTED: Client %s:%d", ip, port);
    return 0;
}
// 记录客户端操作
int log_operation(const char *client_ip, int port, const char *action, const char *file) {
    syslog(LOG_NOTICE, "OPERATION: [%s:%d] %s -> File: '%s'",
           client_ip, port, action, file);
    return 0;
}

// 关闭日志系统
int close_log_system() {
    syslog(LOG_NOTICE, "===== Service Terminated =====");
    closelog();
    return 0;
}

//实际打印函数
int my_log(const char *file,const char* func,int line,int Category,char *msg){
    switch(Category){
    case ERR:
         syslog(LOG_ERR,"ERROR: func:%s file:%s line:%d msg:%s\n"
               ,file,func,line,msg);
        break;
    case WARNING:
        syslog(LOG_WARNING,"WARNING: func:%s file:%s line:%d msg:%s\n"
               ,file,func,line,msg);
        break;
    case INFO:
        syslog(LOG_INFO,"INFORMATION: func:%s file:%s line:%d msg:%s\n"
               ,file,func,line,msg);
        break;
    }

    return 0;
}

